package com.peas.platform.module.icims.base.auth.domain.impl;

import cn.hutool.core.lang.Assert;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.auth.adapter.repository.UserRepository;
import com.peas.platform.module.icims.base.auth.adapter.repository.po.UserPo;
import com.peas.platform.module.icims.base.auth.domain.UserDomain;
import com.peas.platform.module.icims.base.auth.dto.UserDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Component
public class UserDomainImpl extends BaseDomainImpl<UserDto, UserPo> implements UserDomain {

    @Resource
    private UserRepository userRepository;

    @Override
    public BaseRepository<UserPo> getMapper() {
        return userRepository;
    }

    @Override
    public Class<? extends BaseRepository<UserPo>> getRepositoryClass() {
        return UserRepository.class;
    }

    @Transactional
    @Override
    public int save(UserDto dto) {
        int result = super.save(dto);
        List<UserDto> list = this.listByField("username", dto.getUsername());
        Assert.isTrue(list.size() == 1, "用户名重复");
        return result;
    }

    @Transactional
    @Override
    public int updateById(UserDto dto) {
        int result = super.updateById(dto);
        List<UserDto> list = this.listByField("username", dto.getUsername());
        Assert.isTrue(list.size() == 1, "用户名重复");
        return result;
    }

}
